package uit.bus;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


import uit.hcm.views.LazyAdapter;
import uit.hcm.views.ObjViews;
import uit.hcm.views.XMLParser;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;


public class MainActivity extends Activity{

	// All static variables
		static final String URL = "http://api.androidhive.info/music/music.xml";
		// XML node keys
		static final String KEY_SONG = "song"; // parent node
		static final String KEY_ID = "id";
		public static final String KEY_TITLE = "title";
		static final String KEY_URL = "url";
		public static List<String> apps_url;
		public static String KEY_STATIC;
		public static List<ObjViews> listView = new ArrayList<ObjViews>();
		ListView list;
	    LazyAdapter adapter;

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.main_custom);
			
			apps_url = new ArrayList<String>();
			setData();
			ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

			XMLParser parser = new XMLParser();
			InputStream in=getResources().openRawResource(R.raw.listview);
			DocumentBuilder builder;
			Document doc = null;
			try {
				builder = DocumentBuilderFactory
				.newInstance()
				.newDocumentBuilder();
				doc=builder.parse(in, null);
			} catch (ParserConfigurationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			NodeList nl = doc.getElementsByTagName(KEY_SONG);
	
			// looping through all song nodes <song>
			for (int i = 0; i < listView.size(); i++) {

				HashMap<String, String> map = new HashMap<String, String>();
				ObjViews r = (ObjViews)listView.get(i);
				map.put(KEY_TITLE, r.getTitle());
				map.put(KEY_STATIC, r.getImgUrl());
				songsList.add(map);

			}
			

			list=(ListView)findViewById(R.id.list);
			
			// Getting adapter by passing xml data ArrayList
	        adapter=new LazyAdapter(this, songsList);        
	        list.setAdapter(adapter);
	        

	        // Click event for single list row
	        list.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
								

				}
			});	
		}
		
		public void setData(){
			
			ObjViews view = new ObjViews();
			view.setTitle("Vị trí hiện tại");
			view.setImgUrl("location_current");
			listView.add(view);
			
			ObjViews view1 = new ObjViews();
			view1.setTitle("Tuyến xe");
			view1.setImgUrl("lotrinh");
			listView.add(view1);
			
			ObjViews view2 = new ObjViews();
			view2.setTitle("Tìm kiếm");
			view2.setImgUrl("search");
			listView.add(view2);
			
			ObjViews view3 = new ObjViews();
			view3.setTitle("Lộ trình");
			view3.setImgUrl("celltower");
			listView.add(view3);
			
			ObjViews view4 = new ObjViews();
			view4.setTitle("Trợ giúp");
			view4.setImgUrl("gvoice");
			listView.add(view4);
			
			ObjViews view5 = new ObjViews();
			view5.setTitle("Thiết lập");
			view5.setImgUrl("gearth");
			listView.add(view5);
			
			ObjViews view6 = new ObjViews();
			view6.setTitle("Xóa tất cả");
			view6.setImgUrl("icon_twidroyd");
			listView.add(view6);
			
			ObjViews view7 = new ObjViews();
			view7.setTitle("Đến vị trí bắt đầu");
			view7.setImgUrl("buzzdeck_black");
			listView.add(view7);
		}
}
